package org.liukai.ssh_ems.action;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import org.liukai.ssh_ems.entity.po.User;
import org.liukai.ssh_ems.service.UserService;
import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import java.util.Map;

/**
 * @project ssh_ems
 * @create UserAction.java 
 * 
 * @author liukai
 * @date 2016/1/14 15:48
 * @detail 
 */

@Controller
public class UserAction extends ActionSupport {

    @Resource
    private UserService userService;

    private User user;

    //跳转到登录页面
    public String toLogin() {
        return SUCCESS;
    }

    //登录
    public String login() {
//        System.out.println(user.getUsername());
//        System.out.println(user.getPwd());
        User u = userService.queryUserByusername(user);
        if(u != null) {
            if(user.getPwd().equals(u.getPwd())){
                //strust2提供的session对象 是个map
                ActionContext.getContext().getSession().put("currentUser",user);
                return SUCCESS;
            } else {
                //strust2提供的request对象 是个map
                Map<String,Object> request = (Map<String, Object>)ActionContext.getContext().get("request");
                request.put("msg", "密码错误");
                return LOGIN;
            }
        } else {
            Map<String,Object> request = (Map<String, Object>)ActionContext.getContext().get("request");
            request.put("msg", "用户不存在");
            return LOGIN;
        }
    }

    //登出
    public String logout() {
        //删除session中的currentUser
        Map<String, Object> session = ActionContext.getContext().getSession();
        session.remove("currentUser");
        return LOGIN;
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }
}
